package com.htfyun.fragment.toc.adapter;

import android.content.Context;
import android.support.v4.content.ContextCompat;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.ImageView;
import android.widget.TextView;

import com.htfyun.fragment.listener.OnRecyclerViewItemClickListener;
import com.htfyun.utils.DebugLog;

import org.geometerplus.fbreader.bookmodel.TOCTree;
import org.geometerplus.zlibrary.ui.android.R;

import java.util.ArrayList;
import java.util.List;

import me.yokeyword.fragmentation.SupportFragment;

/**
 * Created by sst on 2017/11/3.
 */

public class TOCAdapter extends RecyclerView.Adapter<TOCAdapter.MyViewHolder> {

    private LayoutInflater mInflater;

    private List<TOCTree> mItems = new ArrayList<>();

    private TOCTree selectedTOCTree;
    
    private final Context mContext;

    private OnRecyclerViewItemClickListener mClickListener;

    public TOCAdapter(Context context) {
        mContext = context;
        mInflater = LayoutInflater.from(context);

    }

    public void setDatas(List<TOCTree> beans) {
        mItems.clear();
        mItems.addAll(beans);
        notifyDataSetChanged();
    }
    
    public void setOnRecyclerViewItemClickListener(OnRecyclerViewItemClickListener itemClickListener) {
        this.mClickListener = itemClickListener;
    }

    public void setSelectedTOCTree(TOCTree selectedTOCTree) {
        this.selectedTOCTree = selectedTOCTree;
        notifyDataSetChanged();
    }

    @Override
    public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = mInflater.inflate(R.layout.htfyun_toc_adapter_item, parent, false);
        final MyViewHolder holder = new MyViewHolder(view);
        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                Animation animation = AnimationUtils.loadAnimation(mContext, R.anim.anim_view_click);
                v.startAnimation(animation);
                
                if (mClickListener != null) {
                    mClickListener.onItemClick(holder.getAdapterPosition(), v, holder);
                }
            }
        });
        return holder;
    }

    @Override
    public void onBindViewHolder(MyViewHolder holder, int position) {
        TOCTree item = mItems.get(position);

        DebugLog.e("position = " + position + ", title = " + item.getText()) ;
        holder.tvChapter.setText(item.getText());

        if (item == selectedTOCTree) {
            holder.tvChapter.setTextColor(mContext.getResources().getColor(R.color.green));
        } else {
            holder.tvChapter.setTextColor(mContext.getResources().getColor(R.color.black));
        } 
    }

    @Override
    public int getItemCount() {
        return mItems.size();
    }

    class MyViewHolder extends RecyclerView.ViewHolder {
        private TextView tvChapter;

        public MyViewHolder(View itemView) {
            super(itemView);
            tvChapter = (TextView) itemView.findViewById(R.id.txtChapter);
        }
    }
}
